package com.genexususa.eo.connection.google.test.goals;


import com.genexususa.eo.connection.google.analytics.AnalyticsConnector;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.model.*;

import java.io.IOException;

public class ConsumeGetAllGoals {


    public static void main(String[] args){


        try {

            AnalyticsConnector connector = new AnalyticsConnector();
            Analytics analytics = connector.initializeAnalytics();
            Goals goals = analytics.management().goals().list("52206634", "~all", "~all").execute();

            for (Goal goal : goals.getItems()) {
                System.out.println("Account ID: " + goal.getAccountId());
                System.out.println("Property ID: " + goal.getWebPropertyId());
                System.out.println("Property Internal Id: "
                        + goal.getInternalWebPropertyId());
                System.out.println("View (Profile) ID: " + goal.getProfileId());

                System.out.println("Goal Number: " + goal.getId());
                System.out.println("Goal Name: " + goal.getName());
                System.out.println("Goal Value: " + goal.getValue());
               /* System.out.println("Is Goal Active: " + goal.getActive());
                System.out.println("Goal Type: " + goal.getType());

                System.out.println("Goal Created: " + goal.getCreated());
                System.out.println("Goal Updated: " + goal.getUpdated());

                System.out.println("******************************************************************************");


                Webproperties webproperties =
                        analytics.management().webproperties().list(goal.getAccountId()).execute();

                if (webproperties.getItems().isEmpty()) {
                    System.err.println("No webproperties found");
                } else {
                    printWebProperties(webproperties);
                    String firstWebpropertyId = webproperties.getItems().get(0).getId();

                    // Query profiles collection.
                    Profiles profiles =
                            analytics.management().profiles().list(goal.getAccountId(), firstWebpropertyId).execute();

                    if (profiles.getItems().isEmpty()) {
                        System.err.println("No profiles found");
                    } else {
                        printProfiles(profiles);
                        String firstProfileId = profiles.getItems().get(0).getId();

                        // Query goals collection.
                        Goals goals2 = analytics.management()
                                .goals().list(goal.getAccountId(), firstWebpropertyId, firstProfileId).execute();

                        if (goals2.getItems() == null || goals2.getItems().isEmpty()) {
                            System.err.println("No goals found");
                        } else {
                            printGoals(goals);
                        }
                    }
                }   */

                System.out.println("============================================================================");
                /*if (goal.getUrlDestinationDetails() != null) {
                    printUrlDestinationDetails(goal.getUrlDestinationDetails());

                } else if (goal.getVisitTimeOnSiteDetails() != null) {
                    printVisitTimeOnSiteDetails(goal.getVisitTimeOnSiteDetails());

                } else if (goal.getVisitNumPagesDetails() != null) {
                    printVisitNumPagesDetails(goal.getVisitNumPagesDetails());

                } else if (goal.getEventDetails() != null) {
                    printGoalEventDetails(goal.getEventDetails());
                }    */
            }
        } catch (GoogleJsonResponseException e) {
            System.err.println("There was a service error: "
                    + e.getDetails().getCode() + " : "
                    + e.getDetails().getMessage());
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void printGoalEventDetails(Goal.EventDetails eventDetails) {
        System.out.println("EVENT - Use Event Value: "
                + eventDetails.getUseEventValue());

        if (eventDetails.getEventConditions() != null) {
            System.out.println("Goal Conditions: ");
            for (Goal.EventDetails.EventConditions conditions : eventDetails.getEventConditions()) {
                System.out.println("Type: " + conditions.getType());

                if (conditions.getType().equals("VALUE")) {
                    System.out.println("Comparison Type: "
                            + conditions.getComparisonType());
                    System.out.println("Comparison Value: "
                            + conditions.getComparisonValue());
                } else {
                    System.out.println("matchType: " + conditions.getMatchType());
                    System.out.println("expression: " + conditions.getExpression());
                }
            }
        }
    }

    private static void printVisitNumPagesDetails(Goal.VisitNumPagesDetails
                                                          visitNumPagesDetails) {
        System.out.println("Goal Type:  VISIT_NUM_PAGES");
        System.out.println("VISIT_NUM_PAGES - Comparison Type: "
                +  visitNumPagesDetails.getComparisonType());
        System.out.println("VISIT_NUM_PAGES - Comparison Value: "
                +  visitNumPagesDetails.getComparisonValue());
    }

    private static void printVisitTimeOnSiteDetails(
            Goal.VisitTimeOnSiteDetails visitTimeOnSiteDetails) {

        System.out.println("Goal Type:  VISIT_TIME_ON_SITE");
        System.out.println("VISIT_TIME_ON_SITE - Comparison Type: "
                + visitTimeOnSiteDetails.getComparisonType());
        System.out.println("VISIT_TIME_ON_SITE - Comparison Value: "
                + visitTimeOnSiteDetails.getComparisonValue());
    }

    private static void printUrlDestinationDetails(Goal.UrlDestinationDetails
                                                           destinationDetails) {
        System.out.println("Goal Url: " + destinationDetails.getUrl());
        System.out.println("Case Sensitive: "
                +  destinationDetails.getCaseSensitive());
        System.out.println("Match Type: " +  destinationDetails.getMatchType());
        System.out.println("First Step Required: "
                + destinationDetails.getFirstStepRequired());

        if (destinationDetails.getSteps() != null) {
            System.out.println("Goal Steps: ");
            for (Goal.UrlDestinationDetails.Steps step : destinationDetails.getSteps()) {
                System.out.println("Step Number: " + step.getNumber());
                System.out.println("Name: " + step.getName());
                System.out.println("URL: " + step.getUrl());
            }

        } else {
            System.out.println("No Steps Configured");
        }
    }

    private static void printWebProperties(Webproperties webproperties) {
        System.out.println("\n----- Webproperties Collection -----\n");

        for (Webproperty webproperty : webproperties.getItems()) {
            System.out.println("Account ID: " + webproperty.getAccountId());
            System.out.println("Web Property ID: " + webproperty.getId());
            System.out.println("Web Property Name: " + webproperty.getName());
            System.out.println("Web Property Internal Id: " + webproperty.getInternalWebPropertyId());
            System.out.println("Web Property Website URL: " + webproperty.getWebsiteUrl());
            System.out.println("Web Property Created: " + webproperty.getCreated());
            System.out.println("Web Property Updated: " + webproperty.getUpdated());
        }
    }

    private static void printProfiles(Profiles profiles) {
        System.out.println("\n----- Profiles Collection -----\n");

        for (Profile profile : profiles.getItems()) {
            System.out.println("Account ID: " + profile.getAccountId());
            System.out.println("Web Property ID: " + profile.getWebPropertyId());
            System.out.println("Web Property Internal ID: " + profile.getInternalWebPropertyId());
            System.out.println("Profile ID: " + profile.getId());
            System.out.println("Profile Name: " + profile.getName());

            System.out.println("Profile defaultPage: " + profile.getDefaultPage());
            System.out.println(
                    "Profile Exclude Query Parameters: " + profile.getExcludeQueryParameters());
            System.out.println(
                    "Profile Site Search Query Parameters: " + profile.getSiteSearchQueryParameters());
            System.out.println(
                    "Profile Site Search Category Parameters: " + profile.getSiteSearchCategoryParameters());

            System.out.println("Profile Currency: " + profile.getCurrency());
            System.out.println("Profile Timezone: " + profile.getTimezone());
            System.out.println("Profile Created: " + profile.getCreated());
            System.out.println("Profile Updated: " + profile.getUpdated());
        }
    }


    private static void printGoals(Goals goals) {
        System.out.println("\n----- Goals Collection -----\n");

        for (Goal goal : goals.getItems()) {
            System.out.println("Account ID: " + goal.getAccountId());
            System.out.println("Web Property ID: " + goal.getWebPropertyId());
            System.out.println("Web Property Internal Id: " + goal.getInternalWebPropertyId());
            System.out.println("Profile ID: " + goal.getId());

            System.out.println("Goal Number: " + goal.getId());
            System.out.println("Goal Name: " + goal.getName());
            System.out.println("Is Goal Active: " + goal.getActive());
            System.out.println("Goal Value: " + goal.getValue());
            System.out.println("Goal Type: " + goal.getType());
            System.out.println("Goal Created: " + goal.getCreated());
            System.out.println("Goal Updated: " + goal.getUpdated());

            if (goal.getType().equals("URL_DESTINATION")) {
                printUrlDestinationDetails(goal.getUrlDestinationDetails());

            } else if (goal.getType().equals("VISIT_TIME_ON_SITE")) {
                printVisitTimeOnSiteDetails(goal.getVisitTimeOnSiteDetails());

            } else if (goal.getType().equals("VISIT_NUM_PAGES")) {
                printVisitNumPagesDetails(goal.getVisitNumPagesDetails());

            } else if (goal.getType().equals("EVENT")) {
                printGoalEventDetails(goal.getEventDetails());
            }
        }
    }



}
